package com.luoye.restful.generator;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-10-10
 * Time: 下午7:27
 * To change this template use File | Settings | File Templates.
 */
public class StringGenerator {
    private HashMap<String, String> replaceStrs = new HashMap<String, String>();
    private String templatePath;

    public void addReplaceStr(String keystr, String replaceStr) {
        this.replaceStrs.put(keystr, replaceStr);
    }

    public void addReplaceStr(String keystr, ICodeGenerator codeGenerator) {
        this.replaceStrs.put(keystr, codeGenerator.codeGenerate());
    }

    public void removeReplaceStr(String keystr) {
        this.replaceStrs.remove(keystr);
    }

    public void clearReplaceStrs() {
        this.replaceStrs = new HashMap<String, String>();
    }

    public HashMap<String, String> getReplaceStrs() {
        return replaceStrs;
    }

    public void setReplaceStrs(HashMap<String, String> replaceStrs) {
        this.replaceStrs = replaceStrs;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }


    public StringGenerator() {
    }

    public StringGenerator(HashMap<String, String> replaceStrs, String templatePath, String targetPath) {
        this.replaceStrs = replaceStrs;
        this.templatePath = templatePath;
    }

    public String generate() {
        BufferedReader reader = null;
        StringBuffer writer = null;
        try {
            System.out.println("this.templatePath->"+this.templatePath);
            reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(this.templatePath), "utf-8"));

            writer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (this.replaceStrs != null) {
                    Set<String> keySet = this.replaceStrs.keySet();
                    Iterator<String> keyIterator = keySet.iterator();
                    while (keyIterator.hasNext()) {
                        String keyStr = keyIterator.next();
                        String replaceStr = this.replaceStrs.get(keyStr);
                        System.out.println("replaceStr->"+keyStr+"\n"+replaceStr);
                        if(keyStr!=null&&replaceStr!=null) {
                            line = line.replaceAll("\\$\\{" + keyStr + "\\}", replaceStr);
                        }
                    }
                }
                if(line.indexOf("${")==-1) {
                    writer.append(line + "\n");
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return writer.toString();
    }

    public String  generate(String templatePath) {
        this.templatePath = templatePath;
        return generate();
    }

    public void generate(HashMap<String, String> replaceStrs, String templatePath) {
        this.replaceStrs = replaceStrs;
        this.templatePath = templatePath;
        generate();
    }
}
